package cn.oms.oms.service.impl;

import cn.oms.oms.entity.*;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.*;
import cn.oms.oms.service.IPrescriptionService;
import cn.oms.oms.utils.TimeTool;
import cn.oms.oms.vo.PrescriptionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {

    @Autowired
    private EmrMapper emrMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private PickingMapper pickingMapper;

    @Autowired
    private AdmissionMapper admissionMapper;


    @Autowired
    private AccountMapper accountMapper;


    /**
     * 根据查询条件查询处方列表
     * @param prescriptionVo
     * @return
     */
    @Override
    public PageInfo<Prescription> selectPrescriptionList(PrescriptionVo prescriptionVo) {
        QueryWrapper<Prescription> query = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(prescriptionVo.getSelectInput())){
            query.like("patientName",prescriptionVo.getSelectInput())
                    .or().eq("patientId",prescriptionVo.getSelectInput())
                    .or().eq("emrId",prescriptionVo.getSelectInput());
        }

        if (prescriptionVo.getSendItemPharmacyId()!=null&& !"".equals(prescriptionVo.getSendItemPharmacyId())){
            query.eq("sendItemPharmacyId",prescriptionVo.getSendItemPharmacyId());
        }

        if ( prescriptionVo.getVetFlag()!=null&&!"".equals( prescriptionVo.getVetFlag())){
            query.eq("vetFlag",prescriptionVo.getVetFlag());
        }

        query.eq("deleteFlag",0);
        PageHelper.startPage(prescriptionVo.getPageNum(),prescriptionVo.getPageSize());
        List<Prescription> prescriptionList = prescriptionMapper.selectList(query);

        // 数据格式化
        if (prescriptionList.size()>0&&prescriptionList!=null){
            for (Prescription prescription:prescriptionList){
                // 创建时间格式化
                prescription.setReturnCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(prescription.getCreateTime()));

                // 审方时间格式化
                if (prescription.getVetTransitTime()!=null){
                    prescription.setReturnVetTransitTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(prescription.getVetTransitTime()));
                }

                // 药房名称格式化
                if (prescription.getSendItemPharmacyId()!=null){
                    QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("id",prescription.getSendItemPharmacyId());
                    queryWrapper.eq("deleteFlag",0);
                    Department department = departmentMapper.selectOne(queryWrapper);
                    if (StringUtil.isNotEmpty(department.getDepartmentName())){
                        prescription.setReturnSendItemPharmacyIdName(department.getDepartmentName());
                    }else {
                        prescription.setReturnSendItemPharmacyIdName("未知药房");
                    }
                }else {
                    prescription.setReturnSendItemPharmacyIdName("未知药房");
                }
            }
        }

        return new PageInfo<>(prescriptionList);
    }

    /**
     * 根据处方号查询详情
     * @param prescriptionVo
     * @return
     */
    @Override
    public Prescription selectPrescriptionDetail(PrescriptionVo prescriptionVo) {
        if (prescriptionVo.getPrescriptionId()==null||"".equals(prescriptionVo.getPrescriptionId())){
            throw new ServiceException("参数异常，处方号不能为空");
        }
        QueryWrapper<Prescription> query = new QueryWrapper<>();
        query.eq("prescriptionId",prescriptionVo.getPrescriptionId());
        query.eq("deleteFlag",0);
        Prescription prescription = prescriptionMapper.selectOne(query);
        if (prescription==null){
            throw new ServiceException("参数异常，未找到处方信息");
        }

        // 数据格式化
        prescription.setReturnCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(prescription.getCreateTime()));
        if (prescription.getVetTransitTime()!=null){
            prescription.setReturnVetTransitTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(prescription.getVetTransitTime()));
        }

        // 药房名称格式化
        if (prescription.getSendItemPharmacyId()!=null){
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",prescription.getSendItemPharmacyId());
            queryWrapper.eq("deleteFlag",0);
            Department department = departmentMapper.selectOne(queryWrapper);
            if (StringUtil.isNotEmpty(department.getDepartmentName())){
                prescription.setReturnSendItemPharmacyIdName(department.getDepartmentName());
            }else {
                prescription.setReturnSendItemPharmacyIdName("未知部门");
            }
        }else {
            prescription.setReturnSendItemPharmacyIdName("未知药房");
        }

        return prescription;
    }

    /**
     * 新增处方
     * @param prescriptionVo
     * @return
     */
    @Override
    public String addPrescription(PrescriptionVo prescriptionVo) {
        if (prescriptionVo.getEmrId()==null||"".equals(prescriptionVo.getEmrId())){
            throw new ServiceException("参数异常，病历号不能为空");
        }
        if (prescriptionVo.getSendItemPharmacyId()==null||"".equals(prescriptionVo.getSendItemPharmacyId())){
            throw new ServiceException("参数异常，药房不能为空");
        }

        //新增处方是否成功返回标志
        Boolean returnFlag = false;

        // 根据病历号查找病历信息
        QueryWrapper<Emr> query = new QueryWrapper<>();
        query.eq("emrId",prescriptionVo.getEmrId());
        query.eq("deleteFlag",0);
        Emr emr = emrMapper.selectOne(query);
        if (emr==null){
            throw new ServiceException("参数异常，未找到病历信息");
        }

        // 根据病历ID找当前本次入院的电子病历信息名下是否已经绑定了处方单

        // 如果绑定的处方单未审核，则将这张处方单名下所有的医嘱和账单和缴费单信息进行更改
        // 如果绑定的处方单已审核，则新增一张处方单
        QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emrId",prescriptionVo.getEmrId());
        queryWrapper.eq("deleteFlag",0);
        // 查询病历单名下所有处方单
        List<Prescription> prescriptionList = prescriptionMapper.selectList(queryWrapper);
        // 是否有未审方通过的处方单 标记
        Boolean hasNotVetting = false;
        // 原始医嘱号
        Integer preId = null;

        if (prescriptionList.size()>0&&prescriptionList!=null){
            // 说明病历单名下绑定了处方单
            for (Prescription pre:prescriptionList){
                //判断处方是否审核完成
                if (pre.getVetFlag()==1){
                    //处方已审核通过
                }else{
                    //处方未审核 或审核未通过
                    hasNotVetting = true;
                    preId = pre.getPrescriptionId();
                }
            }
        }else {
            //否则说明病历单名下没有处方单,执行新增处方
        }

        //判断是否有未审核的处方
        if (hasNotVetting){

            // 判断是否拿到了原始处方号
            if (preId!=null){
                // 有未审核处方，更改绑定的处方

                // 查询原始处方
                QueryWrapper<Prescription> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("prescriptionId",preId);
                queryWrapper2.eq("deleteFlag",0);
                Prescription prescription = prescriptionMapper.selectOne(queryWrapper2);
                if (prescription==null){
                    throw new ServiceException("更改处方操作，参数异常，处方单不存在");
                }
                //将原始处方删除
                prescription.setDeleteFlag(1);
                int num = prescriptionMapper.updateById(prescription);
                if (num!=1){
                    throw new ServiceException("数据库异常，修改处方信息失败！！！");
                }

                //执行新增处方操作
                //新增一张处方单
                Prescription prescription1 = new Prescription();
                prescription1.setSendItemPharmacyId(prescriptionVo.getSendItemPharmacyId());
                prescription1.setEmrId(prescription.getEmrId());
                // 根据emrID查询患者信息
                QueryWrapper<Emr> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("emrId",prescription.getEmrId());
                queryWrapper3.eq("deleteFlag",0);
                Emr emr1 = emrMapper.selectOne(queryWrapper3);
                if (emr1==null){
                    throw new ServiceException("参数异常，查询的电子病历不存在");
                }
                prescription1.setPatientId(emr1.getPatientId());
                prescription1.setPatientName(emr1.getPatientName());
                prescription1.setVetFlag(0);
                prescription1.setSendItemPharmacyId(prescriptionVo.getSendItemPharmacyId());
                prescription1.setCreateTime(new Date());
                prescription1.setDeleteFlag(0);
                int insert = prescriptionMapper.insert(prescription1);
                if (insert!=1){
                    throw new ServiceException("数据库异常，新增处方单失败");
                }

                // 获取新增处方之后的处方ID
                Integer prescriptionId = prescription1.getPrescriptionId();
                System.out.println("新增成功！！！新增后的处方号为： = " + prescriptionId);

                // 根据原始处方查询绑定的医嘱 进行更改处方ID操作
                QueryWrapper<Order> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("prescriptionId",preId);
                List<Order> orderList = orderMapper.selectList(queryWrapper1);
                if (orderList.size()>0&&orderList!=null){
                    for (Order order:orderList) {
                        Integer num1 = null;
                        //遍历医嘱更改处方ID
                        order.setPrescriptionId(prescriptionId);
                        //修改医嘱信息
                        num1 = orderMapper.updateById(order);
                        if (num1!=1){
                            throw new ServiceException("数据库异常，修改医嘱信息失败,医嘱号为：{0}",order.getOrderId());
                        }
                    }
                }

                // 根据原始处方查询绑定的账单 进行更改处方ID操作
                QueryWrapper<Bill> queryWrapper4 = new QueryWrapper<>();
                queryWrapper4.eq("prescriptionId",preId);
                List<Bill> billList = billMapper.selectList(queryWrapper4);
                if (billList.size()>0&&billList!=null){
                    for (Bill bill:billList) {
                        Integer num1 = null;
                        //遍历账单更改处方ID
                        bill.setPrescriptionId(prescriptionId);
                        //修改医嘱信息
                        num1 = billMapper.updateById(bill);
                        if (num1!=1){
                            throw new ServiceException("数据库异常，修改账单信息失败，账单号为：{0}",bill.getBillRecordId());
                        }
                    }
                }

                // 新增处方完成 更改标记
                returnFlag = true;

            }

        }else {
            // 没有未审核处方，执行普通新增处方操作
            Prescription prescription = new Prescription();
            prescription.setEmrId(prescriptionVo.getEmrId());
            prescription.setPatientId(emr.getPatientId());
            prescription.setPatientName(emr.getPatientName());
            prescription.setSendItemPharmacyId(prescriptionVo.getSendItemPharmacyId());
            prescription.setVetFlag(0);
            prescription.setCreateTime(new Date());
            prescription.setDeleteFlag(0);
            int insert = prescriptionMapper.insert(prescription);
            if (insert!=1){
                throw new ServiceException("数据库异常，新增处方单失败");
            }

            // 新增处方完成 更改标记
            returnFlag = true;
        }

        // 判断标记是否新增成功
        if (returnFlag){
            return "ok";
        }else {
            throw new ServiceException("数据库新增处方异常，新增处方失败！！！！");
        }
    }

    /**
     * 修改处方
     * @param prescriptionVo
     * @return
     */
    @Override
    public String updatePrescription(PrescriptionVo prescriptionVo) {
        if (prescriptionVo.getPrescriptionId()==null||"".equals(prescriptionVo.getPrescriptionId())){
            throw new ServiceException("参数异常，处方ID不能为空");
        }
        if (prescriptionVo.getSendItemPharmacyId()==null||"".equals(prescriptionVo.getSendItemPharmacyId())){
            throw new ServiceException("参数异常，药房ID不能为空");
        }

        QueryWrapper<Prescription> query = new QueryWrapper<>();
        query.eq("prescriptionId",prescriptionVo.getPrescriptionId());
        query.eq("deleteFlag",0);
        Prescription prescription = prescriptionMapper.selectOne(query);
        if (prescription==null){
            throw new ServiceException("参数异常，处方不存在");
        }
        prescription.setSendItemPharmacyId(prescriptionVo.getSendItemPharmacyId());
        int num = prescriptionMapper.updateById(prescription);
        if (num!=1){
            throw new ServiceException("数据库异常,处方信息修改失败");
        }
        return "ok";
    }

    /**
     * 删除处方
     * @param id
     * @return
     */
    @Override
    public String deletePrescription(Integer id) {
        if (id==null||"".equals(id)){
            throw new ServiceException("参数异常，处方ID为空");
        }

        QueryWrapper<Prescription> query = new QueryWrapper<>();
        query.eq("prescriptionId",id);
        query.eq("deleteFlag",0);
        Prescription prescription = prescriptionMapper.selectOne(query);
        if(prescription==null){
            throw new ServiceException("参数异常，未找到处方");
        }

        prescription.setDeleteFlag(1);
        int num = prescriptionMapper.updateById(prescription);

        if (num!=1){
            throw new ServiceException("数据库异常,处方信息删除失败");
        }
        return "ok";
    }

    /**
     * 审方
     * @param prescriptionVo
     * @return
     */
    @Override
    public String vetting(PrescriptionVo prescriptionVo) {
        if (prescriptionVo.getPrescriptionId()==null||"".equals(prescriptionVo.getPrescriptionId())){
            throw new ServiceException("参数异常，处方号ID不能为空");
        }

        //审方之前判断处方名下是否还有未签名的医嘱
        QueryWrapper<Order> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.eq("prescriptionId",prescriptionVo.getPrescriptionId());
        queryWrapper5.eq("deleteFlag",0);
        List<Order> orderList1 = orderMapper.selectList(queryWrapper5);
        if (orderList1.size()>0&&orderList1!=null){
            //名下有医嘱 查医嘱里面是否有未签名的 有的话就无法审方
            for (Order order:orderList1){
                if (order.getDoctorSignatureFlag()==0){
                    throw new ServiceException("无法审方，处方单还有未签名医嘱");
                }
            }
        }else {
            // 处方ID名下没有医嘱 无法对审方进行任何操作
            throw new ServiceException("无法审方，处方单没有任何医嘱");
        }

        //判断审方标记
        if (prescriptionVo.getVetFlag()==1){
            // 审方通过
            // 更改处方信息
            QueryWrapper<Prescription> query = new QueryWrapper<>();
            query.eq("prescriptionId",prescriptionVo.getPrescriptionId());
            query.eq("deleteFlag",0);
            Prescription prescription = prescriptionMapper.selectOne(query);
            if (prescription==null){
                throw new ServiceException("参数异常，未找到处方");
            }
            prescription.setVetFlag(prescriptionVo.getVetFlag());
            prescription.setVetTransitTime(new Date());
            int num = prescriptionMapper.updateById(prescription);
            if (num!=1){
                throw new ServiceException("数据库异常,处方信息修改失败");
            }

            // 新增捡药发药 和 账单
            // 新增账单
            // 查询医嘱
            QueryWrapper<Order> query1 = new QueryWrapper<>();
            query1.eq("prescriptionId",prescription.getPrescriptionId());
            query1.eq("deleteFlag",0);
            List<Order> orderList = orderMapper.selectList(query1);
            if (orderList.size()>0&&orderList!=null){
                // 查询到处方单 名下有医嘱
                for (Order order:orderList){
                    //遍历所有医嘱 执行新增捡药发药 新增账单操作
                    // 新增捡药发药
                    Picking picking = new Picking();
                    picking.setOrderId(order.getOrderId());
                    picking.setPrescriptionId(order.getPrescriptionId());
                    picking.setPatientId(order.getPatientId());
                    picking.setItemId(order.getItemId());
                    // 查询药品名称
                    QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("itemId",order.getItemId());
                    queryWrapper.eq("deleteFlag",0);
                    Item item = itemMapper.selectOne(queryWrapper);
                    if (item==null){
                        throw new ServiceException("数据库异常,未查询到药品");
                    }
                    picking.setItemDescription(item.getItemName());
                    picking.setItemSpecificationDetail(item.getItemSpecificationDetail());
                    picking.setManufacturerName(item.getManufacturerName());
                    picking.setManufacturerApprovalCode(item.getManufacturerApprovalId());
                    // 发药量
                    if (order.getSendDose()!=null){
                        int sendDose = order.getSendDose().intValue();
                        Integer sendQty = Integer.valueOf(sendDose);
                        picking.setSendQty(sendQty);
                    }
                    // 捡药量
                    picking.setPickingQty(0);
                    picking.setBaseUnitName(order.getSendDoseUnitName());
                    picking.setPickingFlag(0);
                    picking.setPayFlag(0);
                    picking.setSendFlag(0);
                    picking.setDeleteFlag(0);
                    picking.setCreateTime(new Date());
                    // 执行新增捡药发药
                    int num1 = pickingMapper.insert(picking);
                    if (num1!=1){
                        throw new ServiceException("数据库新增异常,新增捡药发药失败");
                    }


                    // 新增账单
                    Bill bill = new Bill();
                    bill.setCreateTime(new Date());
                    bill.setDeleteFlag(0);
                    bill.setPatientId(order.getPatientId());
                    bill.setPrescriptionId(order.getPrescriptionId());
                    bill.setOrderId(order.getOrderId());
                    bill.setItemId(item.getItemId());
                    bill.setItemName(item.getItemName());
                    // 产品单价
                    BigDecimal itemPrice = item.getItemPrice();
                    bill.setItemPrice(itemPrice);
                    bill.setItemPriceUnitName(item.getItemUnitName());
                    // 产品数量
                    int sendDose = order.getSendDose().intValue();
                    Integer sendQty = Integer.valueOf(sendDose);
                    bill.setItemNum(sendQty);
                    // 计算总价
                    BigDecimal totalPrice = itemPrice.multiply(order.getSendDose());
                    bill.setItemTotalPrice(totalPrice);

                    // 账单费用类型
                    // 根据病历号查询病历号
                    QueryWrapper<Emr> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("emrId",order.getEmrId());
                    queryWrapper1.eq("deleteFlag",0);
                    Emr emr = emrMapper.selectOne(queryWrapper1);
                    if (emr==null){
                        throw new ServiceException("参数异常,新增账单对应病历信息未找到");
                    }
                    //根据病历信息查询入院记录
                    QueryWrapper<Admission> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("admissionId",emr.getAdmissionId());
                    queryWrapper2.eq("deleteFlag",0);
                    Admission admission = admissionMapper.selectOne(queryWrapper2);
                    if (admission==null){
                        throw new ServiceException("参数异常,新增账单对应入院登记信息未找到");
                    }
                    //账单费用类型
                    if (admission.getFeeType()==0){
                        bill.setBillFeeTypeName("自费");
                    }
                    // 新增缴费单再回来将缴费单绑定进去执行新增账单
                    //判断医嘱名下是否有缴费单
                    Integer prescriptionId = order.getPrescriptionId();
                    //先根据医嘱绑定的处方找到处方名下所有账单
                    QueryWrapper<Bill> queryWrapper3 = new QueryWrapper<>();
                    queryWrapper3.eq("prescriptionId",prescriptionId);
                    queryWrapper3.eq("deleteFlag",0);
                    List<Bill> billList = billMapper.selectList(queryWrapper3);
                    Boolean hasJournal = false;
                    List<Integer> accountIdList = new ArrayList<>();
                    //判断是否有账单
                    if (billList.size()>0&&billList!=null){
                        //如果有账单了，那么遍历账单找到缴费单
                        for (Bill bill1:billList){
                            if(bill1.getBillingFlag()==1){
                                Integer accountId = bill1.getAccountId();
                                hasJournal = true;
                                accountIdList.add(accountId);
                            }
                        }
                    }else {
                        // 如果还没有账单，就执行新增缴费单
                        Account account = new Account();
                        account.setItemTotalPrice(new BigDecimal(0));
                        if (admission.getFeeType()==0){
                            account.setItemPriceUnitName("元");
                        }
                        account.setPayFlag(0);
                        account.setCreateTime(new Date());
                        account.setDeleteFlag(0);
                        int num2 = accountMapper.insert(account);
                        if (num2!=1){
                            throw new ServiceException("数据库新增异常,新增缴费单失败");
                        }
                        hasJournal=true;
                        accountIdList.add(account.getAccountId());
                    }
                    // 缴费单ID去重
                    accountIdList = accountIdList.stream().distinct().collect(Collectors.toList());
                    // 进行缴费单判断
                    if (hasJournal&&accountIdList.size()==1){
                        //说明找到了缴费单，则将缴费单ID插入账单中
                        bill.setBillingFlag(1);
                        bill.setAccountId(accountIdList.get(0));
                    }else {
                        throw new ServiceException("参数异常,缴费单数据异常，同处方名下存在多个缴费单");
                    }
                    bill.setBillPayFlag(0);
                    // 执行新增账单操作
                    int insert = billMapper.insert(bill);
                    if (insert!=1){
                        throw new ServiceException("数据库新增异常,新增账单失败");
                    }

                    // 根据账单的缴费单ID查询缴费单
                    QueryWrapper<Account> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("accountId",bill.getAccountId());
                    queryWrapper4.eq("deleteFlag",0);
                    Account account = accountMapper.selectOne(queryWrapper4);
                    if (account==null){
                        throw new ServiceException("参数异常，未找到缴费单");
                    }
                    // 根据账单的总价将价格给到缴费单中
                    account.setItemTotalPrice(account.getItemTotalPrice().add(bill.getItemTotalPrice()));
                    int updateAccountReturnNo = accountMapper.updateById(account);
                    if (updateAccountReturnNo!=1){
                        throw new ServiceException("数据库修改异常,修改缴费单总费用失败");
                    }
                }
            }else {
                //处方单中没有医嘱
                return "ok";
            }

        }else if (prescriptionVo.getVetFlag()==2){
            // 退回重审
            // 更改处方信息
            QueryWrapper<Prescription> query = new QueryWrapper<>();
            query.eq("prescriptionId",prescriptionVo.getPrescriptionId());
            query.eq("deleteFlag",0);
            Prescription prescription = prescriptionMapper.selectOne(query);
            if (prescription==null){
                throw new ServiceException("参数异常，未找到处方");
            }
            prescription.setVetFlag(prescriptionVo.getVetFlag());
            int num = prescriptionMapper.updateById(prescription);
            if (num!=1){
                throw new ServiceException("数据库异常,处方信息修改失败");
            }
            // 对处方名下的所有医嘱进行修改，改为未签名
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("prescriptionId",prescriptionVo.getPrescriptionId());
            queryWrapper.eq("deleteFlag",0);
            List<Order> orderList = orderMapper.selectList(queryWrapper);
            // 判断处方名下是否有医嘱
            if (orderList.size()>0&&orderList!=null){
                //若有 遍历医嘱改签名标记
                for (Order order:orderList){
                    if (order.getDoctorSignatureFlag()==1){
                        order.setDoctorSignatureFlag(0);
                        int orderUpdateReturnNo = orderMapper.updateById(order);
                        if (orderUpdateReturnNo!=1){
                            throw new ServiceException("数据库异常,医嘱信息修改签名标记失败");
                        }
                    }
                }
            }

        }else {
            // 参数异常
            throw new ServiceException("参数异常，审方参数异常");
        }

        return "ok";
    }
}
